﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PJ.Data
{
    public abstract class EntityWCFProvider<TEntityData> : ObjectWCFProvider<TEntityData>, IEntityProvider<TEntityData>
        where TEntityData : BaseEntityData, IEntityData, new()
    {
        #region Constructor
        public EntityWCFProvider(IEntityProvider service)
            : base(service)
        {
        }
        #endregion
        #region Interface implementation

        /// <summary>
        /// Gets the data object from the data source
        /// </summary>
        /// <param name="id">Entity id</param>
        /// <returns>The data object if found or throws an exception</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="id"/> is <see langref="null"/> or empty</exception>
        /// <exception cref="DataNotFoundException">If no entity with tis id exists</exception>
        public TEntityData GetById(string id)
        {
            return ToContract(Provider.GetById(id));
        }

        /// <summary>
        /// Gets the data object using cache storage first, if not found in cache gets the data from the provider.
        /// </summary>
        /// <param name="id">Entity id</param>
        /// <returns>The data object if found or throws an exception</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="id"/> is <see langref="null"/> or empty</exception>
        /// <exception cref="DataNotFoundException">If no entity with tis id exists</exception>
        public TEntityData CachedGetById(string id)
        {
            return ToContract(Provider.CachedGetById(id));
        }

        /// <summary>
        /// Finds the data object from the data source
        /// </summary>
        /// <param name="id">Entity id</param>
        /// <returns>The data object if found or returns null</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="id"/> is <see langref="null"/> or empty</exception>
        public TEntityData FindById(string id)
        {
            return ToContract(Provider.FindById(id));
        }

        /// <summary>
        /// Finds the data object using cache storage first, if not found in cache finds the data from the provider.
        /// </summary>
        /// <param name="id">Entity id</param>
        /// <returns>The data object if found or returns null</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="id"/> is <see langref="null"/> or empty</exception>
        public TEntityData CachedFindById(string id)
        {
            return ToContract(Provider.CachedFindById(id));
        }

        #endregion
        #region Interface override
        /// <summary>
        /// Gets the data object from the data source
        /// </summary>
        /// <param name="id">Entity id</param>
        /// <returns>The data object</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="id"/> is <see langref="null"/> or empty</exception>
        /// <exception cref="DataNotFoundException">If no entity with tis id exists</exception>
        IEntityData IEntityProvider.GetById(string id)
        {
            return GetById(id);
        }

        /// <summary>
        /// Gets the data object using cache storage first, if not found in cache gets the data from the provider.
        /// </summary>
        /// <param name="id">Entity id</param>
        /// <returns>The data object if found or throws an exception</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="id"/> is <see langref="null"/> or empty</exception>
        /// <exception cref="DataNotFoundException">If no entity with tis id exists</exception>
        IEntityData IEntityProvider.CachedGetById(string id)
        {
            return CachedGetById(id);
        }

        /// <summary>
        /// Finds the data object from the data source
        /// </summary>
        /// <param name="id">Entity id</param>
        /// <returns>The data object if found or returns null</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="id"/> is <see langref="null"/> or empty</exception>
        IEntityData IEntityProvider.FindById(string id)
        {
            return FindById(id);
        }

        /// <summary>
        /// Finds the data object using cache storage first, if not found in cache finds the data from the provider.
        /// </summary>
        /// <param name="id">Entity id</param>
        /// <returns>The data object if found or returns null</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="id"/> is <see langref="null"/> or empty</exception>
        IEntityData IEntityProvider.CachedFindById(string id)
        {
            return CachedFindById(id);
        }


        #endregion
        #region Public
        #endregion

        #region Protected
        new protected IEntityProvider Provider
        {
            get
            {
                return (IEntityProvider)base._Provider;
            }
        }

        #endregion
        #region Private

        #endregion
        #region Field
        #endregion

    }
}
